<?php // $Id: view.php,v 1.95.2.9 2008/12/31 15:51:28 skodak Exp $

//  Display the matriculation page.

    require_once('../../config.php');
    require_once($CFG->dirroot.'/blocks/matriculation/lib.php');
	require_once($CFG->dirroot.'/enrol/mnet/enrol.php');
	
	$action  = optional_param('action', 0, PARAM_INT);
    $courseid = optional_param('course', 0, PARAM_INT);

    if(!$site = get_site()) {
        redirect($CFG->wwwroot.'/'.$CFG->admin.'/index.php');
    }

    if ($courseid) {
        require_login($courseid);
    } else if ($CFG->forcelogin) {
        require_login();
    }
	
	$navlinks = array();
    $navlinks[] = array('name' => get_string('blockname', 'block_matriculation'),
                        'link' =>'view.php',
                        'type' => 'misc');
	
    // Print title and header
    $navigation = build_navigation($navlinks);
    print_header("$site->shortname", get_string('blockname', 'block_matriculation'), $navigation);
	
	echo '<div id="content" class=" clearfix">';
	
	echo '<h2 class="main">';
	if ($action==1 or $action==3)
		echo get_string('add', 'block_matriculation');
	if ($action==2 or $action==4)
		echo get_string('remove', 'block_matriculation');
	if ($action==6 or $action==7)
		echo get_string('convalidation', 'block_matriculation');
	if ($action==5)
		echo get_string('history', 'block_matriculation');
		
	echo '</h2>';
	
	$toprow[] = new tabobject('historial', $CFG->wwwroot.'/blocks/matriculation/view.php?action=5',get_string('history', 'block_matriculation'));
	$tabs = array($toprow);
	$toprow[] = new tabobject('altas', $CFG->wwwroot.'/blocks/matriculation/view.php?action=1',get_string('add_title', 'block_matriculation'));
	$tabs = array($toprow);	
	$toprow[] = new tabobject('bajas', $CFG->wwwroot.'/blocks/matriculation/view.php?action=2',get_string('remove_title', 'block_matriculation'));
	$tabs = array($toprow);
	$toprow[] = new tabobject('convalidaciones', $CFG->wwwroot.'/blocks/matriculation/view.php?action=6',get_string('convalidation_title', 'block_matriculation'));
	$tabs = array($toprow);
	
	
	if ($action==1 or $action==3)
		$currenttab='altas';
	if ($action==2 or $action==4)
		$currenttab='bajas';
	if ($action==6 or $action==7)
		$currenttab='convalidaciones';
	if ($action==5)
		$currenttab='historial';
	
	print_tabs($tabs, $currenttab);
	
	if ($action==1) { //listado de alumnos para matricular
		
		if (block_matriculation_compare_db_count(1)>0 && count_records('block_matriculation_temp')>0) {
			
			if (block_matriculation_compare_db_count(2)>0 && count_records('block_matriculation_temp')>0) {
				echo '<div style="text-align:center; width:100%">'.get_string('message_comp1', 'block_matriculation').'</div>';
			} else {
			
				$users=block_matriculation_compare_db(1);	
				
				$centers=get_records('block_matriculation_center');
				foreach ($centers as $cent) {
					$url="view.php?action=1&center=".$cent->code;
					echo "<a href=\"$url\">$cent->name</a><br>";
				}
				echo '<br>';
				
				echo '<table style="height:100%;" >';
				echo '<tr>';
				echo '<td class="maincalendar">&nbsp;&nbsp;&nbsp;'.get_string('students_add', 'block_matriculation').'</td>';
				echo '</tr>';
				echo '</table>';
				echo '<br><br>';
				echo '<table border="1" style="text-align:center">';
				echo '<tr>';
				echo '<td><strong>'.get_string('dni', 'block_matriculation').'</strong></td>';
				echo '<td><strong>'.get_string('firstname_lastname', 'block_matriculation').'</strong></td>';
				echo '<td><strong>'.get_string('mail', 'block_matriculation').'</strong></td>';
				echo '<td><strong>'.get_string('module', 'block_matriculation').'</strong></td>';
				echo '<td><strong>'.get_string('cycle', 'block_matriculation').'</strong></td>';
				echo '<td><strong>'.get_string('center', 'block_matriculation').'</strong></td>';
				echo '<td><strong>'.get_string('matriculate', 'block_matriculation').'</strong></td>';
				echo '</tr>';
				echo '<form name="altas" action="view.php?action=3" method="POST" >';

				$control=0;
				foreach ($users as $user) {
					
					if (isset($_GET['center'])) {
						
						if ($user->center_code==$_GET['center']) {
							
							$module=get_record('block_matriculation_module','code', $user->module_code);
							$cycle=get_record('block_matriculation_cycle','code', $user->cycle_code);
							$center=get_record('block_matriculation_center','code', $user->center_code);
							
							echo '<tr>';
							echo '<td>'.$user->dni.'</td>';
							echo '<td>'.$user->firstname.' '.$user->lastname1.' '.$user->lastname2.'</td>';
							echo '<td>'.$user->email.'</td>';
							echo '<td>'.$module->name.'</td>';
							echo '<td>'.$cycle->name.'</td>';
							echo '<td>'.$center->name.'</td>';
							echo '<td><input type="checkbox" name="'.$user->dni.'"  value="'.$user->dni.'"></td>';		
							echo '</tr>';
							$control=1;
							
						}
						
					} else {
						$module=get_record('block_matriculation_module','code', $user->module_code);
						$cycle=get_record('block_matriculation_cycle','code', $user->cycle_code);
						$center=get_record('block_matriculation_center','code', $user->center_code);
						
						echo '<tr>';
						echo '<td>'.$user->dni.'</td>';
						echo '<td>'.$user->firstname.' '.$user->lastname1.' '.$user->lastname2.'</td>';
						echo '<td>'.$user->email.'</td>';
						echo '<td>'.$module->name.'</td>';
						echo '<td>'.$cycle->name.'</td>';
						echo '<td>'.$center->name.'</td>';
						echo '<td><input type="checkbox" name="'.$user->dni.'"  value="'.$user->dni.'"></td>';		
						echo '</tr>';
						$control=1;
					}
					
				}
				if ($control==1) {
					echo '<tr>';
					echo '<td colspan="7" style=" text-align: center"><br><input type="submit" value="'.get_string('matriculate', 'block_matriculation').'"><br><br></td>';
					echo '</tr>';
				}
				
				echo '</table>';
				echo '</form>';
			}
			
		} else {
			echo '<div style="text-align:center; width:100%">'.get_string('matriculate_not_exist', 'block_matriculation').'</div>';
		}
		
	} else if ($action==2) { //listado de alumnos para desmatricular
	
		if (block_matriculation_compare_db_count(2)>0 && count_records('block_matriculation_temp')>0) {
		
			$users=block_matriculation_compare_db(2);
	
			$centers=get_records('block_matriculation_center');
			foreach ($centers as $cent) {
				$url="view.php?action=2&center=".$cent->code;
				echo "<a href=\"$url\">$cent->name</a><br>";
			}
			echo '<br>';
			
			echo '<table style="height:100%;" >';
			echo '<tr>';
			echo '<td class="maincalendar">&nbsp;&nbsp;&nbsp;'.get_string('students_remove', 'block_matriculation').'</td>';
			echo '</tr>';
			echo '</table>';
			echo '<br><br>';
			echo '<table border="1" style="text-align:center">';
			echo '<tr>';
			echo '<td><strong>'.get_string('dni', 'block_matriculation').'</strong></td>';
			echo '<td><strong>'.get_string('firstname_lastname', 'block_matriculation').'</strong></td>';
			echo '<td><strong>'.get_string('mail', 'block_matriculation').'</strong></td>';
			echo '<td><strong>'.get_string('module', 'block_matriculation').'</strong></td>';
			echo '<td><strong>'.get_string('cycle', 'block_matriculation').'</strong></td>';
			echo '<td><strong>'.get_string('center', 'block_matriculation').'</strong></td>';
			echo '<td><strong>'.get_string('select', 'block_matriculation').'</strong></td>';
			echo '</tr>';
			echo '<form name="altas" action="view.php?action=4" method="POST" >';
			$control=0;
			
			foreach ($users as $user) {
			
				if (isset($_GET['center'])) {
				
					if ($user->center_code==$_GET['center']) {
						
						$users_details=get_record('block_matriculation_user','dni',$user->dni);
						$module=get_record('block_matriculation_module','code',$user->module_code);
						$cycle=get_record('block_matriculation_cycle','code',$user->cycle_code);
						$center=get_record('block_matriculation_center','code',$user->center_code);
						
						echo '<tr>';
						echo '<td>'.$user->dni.'</td>';
						echo '<td>'.$users_details->firstname.' '.$users_details->lastname1.' '.$users_details->lastname2.'</td>';
						echo '<td>'.$users_details->email.'</td>';
						echo '<td>'.$module->name.'</td>';
						echo '<td>'.$cycle->name.'</td>';
						echo '<td>'.$center->name.'</td>';
						echo '<td><input type="checkbox" name="'.$user->dni.'" value="'.$user->dni.'"></td>';
						echo '</tr>';
						$control=1;
						
					}
				} else {
					
					$users_details=get_record('block_matriculation_user','dni',$user->dni);
					$module=get_record('block_matriculation_module','code',$user->module_code);
					$cycle=get_record('block_matriculation_cycle','code',$user->cycle_code);
					$center=get_record('block_matriculation_center','code',$user->center_code);
					
					echo '<tr>';
					echo '<td>'.$user->dni.'</td>';
					echo '<td>'.$users_details->firstname.' '.$users_details->lastname1.' '.$users_details->lastname2.'</td>';
					echo '<td>'.$users_details->email.'</td>';
					echo '<td>'.$module->name.'</td>';
					echo '<td>'.$cycle->name.'</td>';
					echo '<td>'.$center->name.'</td>';
					echo '<td><input type="checkbox" name="'.$user->dni.'" value="'.$user->dni.'"></td>';
					echo '</tr>';
					$control=1;
				}
			}
			
			if ($control==1) {
				echo '<tr>';
				echo '<td colspan="7" style=" text-align: center"><br><input type="submit" value="'.get_string('unmatricula', 'block_matriculation').'"><br><br></td>';
				echo '</tr>';
			}
			
			echo '</table>';
			echo '</form>';
		} else {
			
			echo '<div style="text-align:center; width:100%">'.get_string('unmatriculate_not_exist', 'block_matriculation').'</div>';
			
		}
		
	} else if ($action==3) { //realizar matricula en alumnos
		
		$users=block_matriculation_compare_db(1);
		foreach ($users as $user) {
			if (isset($_POST[$user->dni])) { // se dan de baja a los alumnos marcados
			
				//se eliminan de la tabla block_matriculation_temp y se almacena en block_matriculation
				$control1=block_matriculation_write_user_db($user);
				
				//se almacena en el log la matriculación
				$log = new object;	
				$log->dni=$user->dni;
				$log->action_code=1;
				$log->cycle_code=$user->cycle_code;
				$log->module_code=$user->module_code;
				$log->center_code=$user->center_code;
				$log->time=time();
				$control2=insert_record('block_matriculation_log', $log);
				
				//se crea el objeto para almacenar al usuario en caso de que no exista en la plataforma
				$user_store= new object;
				$user_store->username = strtolower($user->dni);
				$user_store->password =  hash_internal_user_password(strtolower($user->dni));
				$user_store->firstname = $user->firstname;
				$user_store->lastname = $user->lastname1 .' '. $user->lastname2;
	            $user_store->email = $user->email;
				$user_store->lang = 'es_es_utf8';
				$user_store->confirmed=1;
				$user_store->mnethostid=1;
				$user_store->ajax='false';
				$user_store->timemodified=time();
				$user_store->imagealt='';
				$user_store->description='';
				
				$rol=get_record('role','shortname', 'student');
				$course=get_record('course','idnumber',$user->module_code.$user->center_code);
				
				//se comprueba si el usuario existe, en caso de que no exista se almacena
				$user_stored=0;
				$user_exist=get_record('user', 'username',$user_store->username);
				if (empty($user_exist)) { 
					$user_stored=1;
					$user_exist=insert_record('user',$user_store);
				}
				
				if ($user_stored==1)
					$userid=$user_exist;
				else
					$userid=$user_exist->id;
				
				$context=get_record('context','instanceid',$course->id,'contextlevel',50);
				
				//se enrola al usuario en el curso que corresponde
				$role_assignment = new object;	
				$role_assignment->roleid = $rol->id;
				$role_assignment->contextid = $context->id;
				$role_assignment->userid = $userid;
				$role_assignment->hidden = 'false';
				$role_assignment->timestart = time();
				$role_assignment->timemodified = time();
				$role_assignment->enrol = 'manual';
				
				$control3=insert_record('role_assignments',$role_assignment);
				
				
				
				if ($control3)
					echo '<br> '.get_string('alum', 'block_matriculation').' '.$user->firstname.' '.$user->lastname1.' '.$user->lastname2.'('.$user->dni.') '.get_string('has_matriculate', 'block_matriculation').' '.$course->fullname;
				else
					echo '<br> '.get_string('alum', 'block_matriculation').' '.$user->firstname.' '.$user->lastname1.' '.$user->lastname2.'('.$user->dni.') '.get_string('hasnt_matriculate', 'block_matriculation').' '.$course->fullname;
				
			} 
			
			delete_records('block_matriculation_temp');
			
		}
		
	} else if ($action==4) { // realizar bajas en alumnos
		
		$users=block_matriculation_compare_db(2);
		
		foreach ($users as $user) {
			
			if (isset($_POST[$user->dni])) { 
				
				$users_details=get_record('block_matriculation_user','dni',$user->dni);
				
				//se eliminan de la tabla block_matriculation
				$control1=delete_records('block_matriculation','dni',$user->dni,'cycle_code',$user->cycle_code,'module_code',$user->module_code);
				
				//se almacena en el log la matriculación
				$log = new object;	
				$log->dni=$user->dni;
				$log->action_code=2;
				$log->cycle_code=$user->cycle_code;
				$log->module_code=$user->module_code;
				$log->center_code=$user->center_code;
				$log->time=time();
				$control2=insert_record('block_matriculation_log', $log);
				
				//se elimina al usuario del curso en el que esta enrolado
				
				//se obtiene el id del usuario a deshenrolar
				$user_real=get_record('user','username',strtolower($user->dni));
				//se obtiene el curso (contextid) en el que se va a deshenrolar
				$course=get_record('course','idnumber',$user->module_code.$user->center_code);
				$context=get_record('context','instanceid',$course->id,'contextlevel',50);
				//se elimina al usuario de dicha tabla
				$control4=delete_records('role_assignments','userid',$user_real->id,'contextid',$context->id,'roleid',5);
				
				if ($control4)
					echo '<br>'.get_string('alum', 'block_matriculation').' '.$users_details->firstname.' '.$users_details->lastname1.' '.$users_details->lastname2.'('.$user->dni.') '.get_string('has_matriculate', 'block_matriculation').' '.$course->fullname;
				else
					echo '<br>'.get_string('alum', 'block_matriculation').' '.$users_details->firstname.' '.$users_details->lastname1.' '.$users_details->lastname2.'('.$user->dni.') '.get_string('hasnt_matriculate', 'block_matriculation').' '.$course->fullname;
				
			} 
		}
	} else if ($action==5) { //listado del historial
			
		echo '<div style="text-align:center; ">';
		echo '<table width=100%>';
		echo '<tr>';
		echo "<td class='maincalendar' width=33%><a href=\"view.php?action=5&list=1\">".get_string('add_title', 'block_matriculation')."</a></td>";
		echo "<td class='maincalendar' width=33%><a href=\"view.php?action=5&list=2\">".get_string('remove_title', 'block_matriculation')."</a></td>";
		echo "<td class='maincalendar' width=33%><a href=\"view.php?action=5&list=3\">".get_string('convalidation_title', 'block_matriculation')."</a></td>";
		echo '</tr>';
		echo '</table>';
		echo '</div><br><br>';
		
		
		
		if (isset($_GET['list'])) { 
			
			$centers=get_records('block_matriculation_center');
			
			foreach ($centers as $cent) {
				$url="view.php?action=5&list=".$_GET['list']."&center=".$cent->code;
				echo "<a href=\"$url\">$cent->name</a><br>";
			}
			echo '<br>';
			
			
			if (isset($_GET['center']))
			{	
				$url="view.php?action=5&list=".$_GET['list']."&center=".$_GET['center'];
				echo '<table>';
				echo "<form name=\"date\" method=\"post\" action=\"$url\">";
				echo '<tr>';
				echo '<td>'.get_string('initial_date', 'block_matriculation').'</td>';
				echo '<td><input type="text" name="initial_date"/>(aaaa-mm-dd)</td>';
				echo '<tr>';
				echo '<td>'.get_string('final_date', 'block_matriculation').'</td>';
				echo '<td><input type="text" name="final_date"/>(aaaa-mm-dd)</td>';
				echo '</tr>';
				echo '<tr><td colspan=2 style="text-align:center"><input type="submit" value="'.get_string('filter', 'block_matriculation').'"/></td></tr>';
				echo "</form>";
				echo '<table><br><br>';
			}
			
			
			if (isset($_GET['center'])) {
				if (isset($_POST['initial_date']) and isset($_POST['final_date'])) {
					$lim_inf_fec=strtotime($_POST['initial_date']);
					$lim_final_fec=strtotime($_POST['final_date']);
					
					$users=get_records_sql('SELECT * FROM mdl_block_matriculation_log WHERE action_code='.$_GET['list'].' AND center_code='.$_GET['center'].' AND time>='.$lim_inf_fec.' AND time<='.$lim_final_fec);
				} else {
					$users=get_records_sql('SELECT * FROM mdl_block_matriculation_log WHERE action_code='.$_GET['list'].' AND center_code='.$_GET['center']);
				}
			}	
			else
				$users=get_records_sql('SELECT * FROM mdl_block_matriculation_log WHERE action_code='.$_GET['list']);
			
			if (!empty($users))
			{
				
				echo '<table border="1" style="text-align:center">';
				echo '<tr>';
				echo '<td><strong>'.get_string('dni', 'block_matriculation').'</strong></td>';
				echo '<td><strong>'.get_string('firstname', 'block_matriculation').'</strong></td>';
				echo '<td><strong>'.get_string('lastnames', 'block_matriculation').'</strong></td>';
				echo '<td><strong>'.get_string('mail', 'block_matriculation').'</strong></td>';
				echo '<td><strong>'.get_string('module', 'block_matriculation').'</strong></td>';
				echo '<td><strong>'.get_string('cycle', 'block_matriculation').'</strong></td>';
				echo '<td><strong>'.get_string('center', 'block_matriculation').'</strong></td>';
				echo '</tr>';
				
				foreach ($users as $user) {
				
					$user_details=get_record('block_matriculation_user','dni',$user->dni);
					$module=get_record('block_matriculation_module','code',$user->module_code);
					$cycle=get_record('block_matriculation_cycle','code',$user->cycle_code);
					$center=get_record('block_matriculation_center','code',$user->center_code);
					
					echo '<tr>';
					echo '<td>'.$user->dni.'</td>';
					
					if (!empty($user_details)) {
						echo '<td>'.$user_details->firstname.'</td>';
						echo '<td>'.$user_details->lastname1.' '.$user_details->lastname2.'</td>';
						echo '<td>'.$user_details->email.'</td>';
					} else {
						echo '<td></td>';
						echo '<td></td>';
						echo '<td></td>';
					}
					
					echo '<td>'.$module->name.'</td>';
					echo '<td>'.$cycle->name.'</td>';
					echo '<td>'.$center->name.'</td>';
					echo '</tr>';
				}
				echo '</table>';
			}
		}
		
		
	} else if ($action==6) { //listado de alumanos para convalidar
	
		if (block_matriculation_compare_db_count(2)>0 && count_records('block_matriculation_temp')>0) {
		
			$users=block_matriculation_compare_db(2);
			
			echo '<table style="height:100%;" >';
			echo '<tr>';
			echo '<td class="maincalendar">&nbsp;&nbsp;&nbsp;'.get_string('convalidate_alumns', 'block_matriculation').' </td>';
			echo '</tr>';
			echo '</table>';
			echo '<br><br>';
			echo '<table border="1" style="text-align:center">';
			echo '<tr>';
			echo '<td><strong>'.get_string('dni', 'block_matriculation').'</strong></td>';
			echo '<td><strong>'.get_string('firstname_lastname', 'block_matriculation').'</strong></td>';
			echo '<td><strong>'.get_string('mail', 'block_matriculation').'</strong></td>';
			echo '<td><strong>'.get_string('module', 'block_matriculation').'</strong></td>';
			echo '<td><strong>'.get_string('cycle', 'block_matriculation').'</strong></td>';
			echo '<td><strong>'.get_string('center', 'block_matriculation').'</strong></td>';
			echo '<td><strong>'.get_string('select', 'block_matriculation').'</strong></td>';
			echo '</tr>';
			echo '<form name="altas" action="view.php?action=7" method="POST" >';
			
			foreach ($users as $user) {
			
				$users_details=get_record('block_matriculation_user','dni',$user->dni);
				$module=get_record('block_matriculation_module','code',$user->module_code);
				$cycle=get_record('block_matriculation_cycle','code',$user->cycle_code);
				$center=get_record('block_matriculation_center','code',$user->center_code);
				
				echo '<tr>';
				echo '<td>'.$user->dni.'</td>';
				echo '<td>'.$users_details->firstname.' '.$users_details->lastname1.' '.$users_details->lastname2.'</td>';
				echo '<td>'.$users_details->email.'</td>';
				echo '<td>'.$module->name.'</td>';
				echo '<td>'.$cycle->name.'</td>';
				echo '<td>'.$center->name.'</td>';
				echo '<td><input type="checkbox" name="'.$user->dni.'" value="'.$user->dni.'"></td>';
				echo '</tr>';
			}
			
			echo '<tr>';
			echo '<td colspan="8" style=" text-align: center"><br><input type="submit" value="'.get_string('convalidate', 'block_matriculation').'"><br><br></td>';
			echo '</tr>';
			echo '</table>';
			echo '</form>';
		} else {
			
			echo '<div style="text-align:center; width:100%">'.get_string('not_data_convalidate', 'block_matriculation').'</div>';
			
		}
		
	} else if ($action==7) { //realizar convalidación de alumnos
		
		$users=block_matriculation_compare_db(2);
		
		foreach ($users as $user) {
			
			if (isset($_POST[$user->dni])) { 
				
				$users_details=get_record('block_matriculation_user','dni',$user->dni);
				
				//se eliminan de la tabla block_matriculation
				$control1=delete_records('block_matriculation','dni',$user->dni,'cycle_code',$user->cycle_code,'module_code',$user->module_code);
				
				//se almacena en el log la matriculación
				$log = new object;	
				$log->dni=$user->dni;
				$log->action_code=3;
				$log->cycle_code=$user->cycle_code;
				$log->module_code=$user->module_code;
				$log->center_code=$user->center_code;
				$log->time=time();
				$control2=insert_record('block_matriculation_log', $log);
				
				//se elimina al usuario del curso en el que está enrolado
				
				//se obtiene el id del usuario a deshenrolar
				$user_real=get_record('user','username',strtolower($user->dni));
				//se obtiene el curso (contextid) en el que se va a deshenrolar
				$course=get_record('course','idnumber',$user->module_code.$user->center_code);
				$context=get_record('context','instanceid',$course->id,'contextlevel',50);
				//se elimina al usuario de dicha tabla
				$control4=delete_records('role_assignments','userid',$user_real->id,'contextid',$context->id,'roleid',5);
				
				if ($control4)
					echo '<br>'.get_string('alum', 'block_matriculation').' '.$users_details->firstname.' '.$users_details->lastname1.' '.$users_details->lastname2.'('.$user->dni.') '.get_string('has_matriculate', 'block_matriculation').' '.$course->fullname;
				else
					echo '<br> '.get_string('alum', 'block_matriculation').' '.$users_details->firstname.' '.$users_details->lastname1.' '.$users_details->lastname2.'('.$user->dni.') '.get_string('hasnt_matriculate', 'block_matriculation').' '.$course->fullname;
				
			} else if (isset($_POST[$cad2])) { 
			
				$users_details=get_record('block_matriculation_user','dni',$user->dni);
				
				//se eliminan de la tabla block_matriculation
				$control1=delete_records('block_matriculation','dni',$user->dni,'cycle_code',$user->cycle_code,'module_code',$user->module_code);
				
				//se almacena en el log la matriculación
				$log = new object;	
				$log->dni=$user->dni;
				$log->action_code=3;
				$log->cycle_code=$user->cycle_code;
				$log->module_code=$user->module_code;
				$log->center_code=$user->center_code;
				$log->time=time();
				$control2=insert_record('block_matriculation_log', $log);
				
				//se elimina al usuario del curso en el que esta enrolado
				
				//se obtiene el id del usuario a deshenrolar
				$user_real=get_record('user','username',strtolower($user->dni));
				//se obtiene el curso (contextid) en el que se va a deshenrolar
				$course=get_record('course','idnumber',$user->module_code.$user->center_code);
				$context=get_record('context','instanceid',$course->id,'contextlevel',50);
				//se elimina al usuario de dicha tabla
				$control4=delete_records('role_assignments','userid',$user_real->id,'contextid',$context->id,'roleid',5);
				
				if ($control4)
					echo '<br>'.get_string('alum', 'block_matriculation').' '.$users_details->firstname.' '.$users_details->lastname1.' '.$users_details->lastname2.'('.$user->dni.') '.get_string('has_matriculate', 'block_matriculation').' '.$course->fullname;
				else
					echo '<br> '.get_string('alum', 'block_matriculation').' '.$users_details->firstname.' '.$users_details->lastname1.' '.$users_details->lastname2.'('.$user->dni.') '.get_string('hasnt_matriculate', 'block_matriculation').' '.$course->fullname;
				
			}
				
		}
	}
	
    print_footer();

?>
